package com.topnet.leaderhelp.utils;

import android.content.Context;


import com.blankj.utilcode.util.ToastUtils;
import com.topnet.leaderhelp.utils.xfyun.ResultCallback;
import com.topnet.leaderhelp.utils.xfyun.XFApiUtils;

import org.bouncycastle.crypto.InvalidCipherTextException;


import java.security.NoSuchAlgorithmException;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import ut.pcmspf.common.security.security.SM3Utils;

/**
 * @Author : 王亚东
 * @Time : On 2021/10/22 11:25
 * @Description : 安卓原生和flutter交互通道
 *
 *
 * @UpdateUser: 下雨
 * @UpdateDate: 2022-02-22 14:52
 * @UpdateRemark:   1、添加 科大讯飞 语音识别 接口
 *                  2、xfVoiceStart  开始录音
 *                  3、xfVoiceStop   结束录音
 *                  4、xfVoiceCancel 取消录音
 *                  5、xfVoiceGetResult  获取录音结果
 *                  6、xfVoiceSetLanguage    语言设置
 *                  7、xfVoiceSetPunctuation 设置是否有标点符号
 *                  8、xfVoiceSetVoiceBOS    设置语音前端点
 *                  9、xfVoiceSetVoiceEOS    设置语音后端点
 * @Version: 1.1
 */
public class MethodChannelUtils {

    public void init(Context context, BinaryMessenger messenger) {
        MethodChannel methodChannel = new MethodChannel(messenger, "com.topnet.xxx");

        methodChannel.setMethodCallHandler((call, result) -> {

            switch (call.method) {
                case "sm3EncryptText":

                    String plainText = call.arguments();
                    String enResult = SM3Utils.encrypt(plainText);

                    result.success(enResult);
                    break;
                case "sm2EncryptText":

                    String plainText2 = call.argument("plainText");
                    String publicKey = call.argument("publicKey");
                    String enResult2;
                    System.out.println("plainText: " + plainText2);
                    System.out.println("publicKey: " + publicKey);
                    try {
                        Sm2Engine sm2Engine = new Sm2Engine();
                        enResult2 = sm2Engine.encodeDataWithPublicKey(plainText2, publicKey);

                        result.success(enResult2);
                    } catch (NoSuchAlgorithmException | InvalidCipherTextException e) {
                        e.printStackTrace();
                        result.error("SM2加密异常", e.getLocalizedMessage(), e.getCause());
                    }
                    break;

                case "xfVoiceStart": // 开始录音

                     XFApiUtils.getInstance(context).start(new ResultCallback() {
                        @Override
                        public void callback(String str) {
                            methodChannel.invokeMethod("xfVoiceSetResult", str);
//                          result.success(str);
                        }
                    });
                    break;
                case "xfVoiceStop": // 结束录音
                    XFApiUtils.getInstance(context).stop();
                    result.success(true);
                    break;
                case "xfVoiceCancel": // 取消录音
                    XFApiUtils.getInstance(context).cancel();
                    result.success(true);
                    break;
                case "xfVoiceGetResult": // 获取语音结果
                    String str = XFApiUtils.getInstance(context).getResult();
                    result.success(str);
                    break;
                case "xfVoiceSetLanguage": // 语言设置
                    String language = call.argument("language");
                    if (language == null || language.length() == 0) {
                        return;
                    }
                    XFApiUtils.getInstance(context).setLanguage(language);
                    result.success(true);
                    break;

                case "xfVoiceSetPunctuation": // 标点符号 （默认有）,设置为"0"返回结果无标点,设置为"1"返回结果有标点
                    String punctuation = call.argument("punctuation");
                    if (punctuation != "0" || punctuation != "1") {
                        return;
                    }
                    XFApiUtils.getInstance(context).setPunctuation(punctuation);
                    result.success(true);
                    break;
                case "xfVoiceSetVoiceBOS": // 设置语音前端点:静音超时时间(0-10000)，即用户多长时间不说话则当做超时处理
                    String bos = call.argument("bos");
                    XFApiUtils.getInstance(context).setVoiceBOS(bos);
                    result.success(true);
                    break;
                case "xfVoiceSetVoiceEOS": // 设置语音后端点:后端点静音检测时间(0-10000)，即用户停止说话多长时间内即认为不再输入， 自动停止录音
                    String eos = call.argument("eos");
                    XFApiUtils.getInstance(context).setVoiceEOS(eos);
                    result.success(true);
                    break;
            }
        });
    }


}
